/**
 * Created by hank-leihan on 16/11/29.
 */
var shares = {};

mui.plusReady(function() {
    plus.share.getServices(function(s) {
        if(s && s.length > 0) {
            for(var i = 0; i < s.length; i++) {
                var t = s[i];
                shares[t.id] = t;
            }
        }
    }, function() {
        //console.log("获取分享服务列表失败");
    });
});

//分享链接点击事件
document.getElementById("jubao").addEventListener('tap', function() {
    var ids = [{
            id: "weixin",
            ex: "WXSceneSession"
        }, {
            id: "weixin",
            ex: "WXSceneTimeline"
        },
            {
                id: "sinaweibo"
            },
            {
                id: "qq"
            }],
        bts = [{
            title: "发送给微信好友"
        }, {
            title: "分享到微信朋友圈"
        },
            {
                title: "分享到新浪微博"
            },
            {
                title: "分享到QQ"
            }];
    plus.nativeUI.actionSheet({
        cancel: "取消",
        buttons: bts
    }, function(e) {
        var i = e.index;
        if(i > 0) {
            var s_id = ids[i - 1].id;
            var share = shares[s_id];
            if(share) {
                if(share.authenticated) {
                    shareMessage(share, ids[i - 1].ex);
                } else {
                    share.authorize(function() {
                        shareMessage(share, ids[i - 1].ex);
                    }, function(e) {
                        //console.log("认证授权失败：" + e.code + " - " + e.message);
                    });
                }
            } else {
                mui.toast("无法获取分享服务，请检查manifest.json中分享插件参数配置，并重新打包")
            }
        }
    });
});

function shareMessage(share, ex) {
    var msg = {
        extra: {
            scene: ex
        }
    };
    if(share_fakeid.substring(0,1) == "A"){
        msg.href = "http://api.chinaola.net/ola/test/Index/person.html?fakeid="+share_fakeid;
    }else{
        msg.href = "http://api.chinaola.net/ola/test/Index/group.html?fakeid="+share_fakeid;
    }
    msg.title = content_title;
    msg.content = content_content;
    if(~share.id.indexOf('sinaweibo')) {
        msg.content = content_title+"--在客户端查看详情。"+msg.href;
        msg.thumbs = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
    }
    else{
        msg.pictures = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
        msg.thumbs = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
    }
    share.send(msg, function() {
        //console.log("分享到\"" + share.description + "\"成功！ ");
    }, function(e) {
        //console.log("分享到\"" + share.description + "\"失败: " + e.code + " - " + e.message);
    });
}
